<?php
namespace app\adminbackstage\service;


use app\adminbackstage\model\RoleUser;
use app\adminbackstage\model\Menu;
use app\adminbackstage\model\Role;
use app\adminbackstage\model\Role_menu;
use app\adminbackstage\model\User;
use app\Response;

class JurisdictionService
{

    /*** 权限菜单列表
     * @param $uid int
     * @return array
     */
    public static function jurosdictionService($uid){
        $role_user = new RoleUser();
        $menu = new Menu();
        $where_u[] = ["uid",'=',$uid];
        $find = User::info($where_u);
        if(empty($find)){
            return false;
        }
        else{
            $find = $find->toArray();
            if($find['enable']!=1){
                return false;
            }
        }
        if($uid != 1){
            $where[] = ['m.enable','=',1];
            $where[] = ['r.enable','=',1];
            $where[] = ['me.type','=',1];
            $where[] = ['r.uid','=',$uid];
            $listinfo = $role_user->jurisdiction($where)->toArray();
            $jurisdiction = array();

            if(!empty($listinfo)){

                foreach ($listinfo as $key=>$val){
                    if(empty($jurisdiction[$val['mid']])  && $val['jur']==1 && $val['mbid']==0){
                        $jurisdiction[$val['mid']] = array('menu_name'=>$val['menu_name'],
                            'superior_id'=>$val['superior_id'],
                            'link'=>$val['link'],
                            'exhibition'=>$val['exhibition'],
                            'component'=>$val['component'],
                            'sort'=>$val['sort'],
                            'jur'=>$val['jur'],
                            'rid'=>$val['rid'],
                            'mid'=>$val['mid'],
                            'Icon'=>$val['Icon'],
                            'button'=>array()
                        );
                    }
                }

                foreach ($listinfo as $key=>$val){
                    if(!empty($jurisdiction[$val['mid']])  && $val['jur']==1 && $val['mbid']!=0){
                        if($val['benable']==1){
                            array_push($jurisdiction[$val['mid']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }
                    }
                }

                $where_all[] = ['enable','=',1];
                $where_all[] = ['superior_id','=',0];
                $where_all[] = ['type','=',1];
                $menuAll = $menu->menuAll($where_all)->toArray();

                foreach($menuAll as $k=>$v){

                    foreach ($jurisdiction as $key_=>$val_){

                        if($val_['superior_id'] == $v['id']){
                            $jurisdiction[$val_['superior_id']] = array('menu_name'=>$v['menu_name'],
                                'superior_id'=>0,
                                'link'=>$v['link'],
                                'exhibition'=>$v['exhibition'],
                                'component'=>$v['component'],
                                'sort'=>$v['sort'],
                                'jur'=>'',
                                'rid'=>'',
                                'mid'=>$v['id'],
                                'Icon'=>$v['Icon'],
                                'button'=>array()
                            );
                        }
                    }
                }

            }
            else{
                return json(['code' => 0, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
            }
        }
        else{
            //admin 获取所有菜单
            $where = [];
            $where[] = ['me.enable','=',1];
            $where[] = ['me.type','=',1];
            $listinfo = $role_user->adminJurisdiction($where);

            if(!empty($listinfo)){
                $listinfo = $listinfo->toArray();
            }
            else{
                return json(['code' => 0, 'msg' => lang('无菜单'), 'data' => '', 'count' => '']);
            }
            $jurisdiction = array();
            foreach ($listinfo as $key=>$val){
                if($val['superior_id']!=0){
                    if(empty($jurisdiction[$val['id']]) ){
                        $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                            'superior_id'=>$val['superior_id'],
                            'link'=>$val['link'],
                            'exhibition'=>$val['exhibition'],
                            'component'=>$val['component'],
                            'sort'=>$val['sort'],
                            'jur'=>1,
                            'rid'=>'',
                            'mid'=>$val['id'],
                            'Icon'=>$val['Icon'],
                            'button'=>array()
                        );
                    }
                }
                else{
                    $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                        'superior_id'=>0,
                        'link'=>$val['link'],
                        'exhibition'=>$val['exhibition'],
                        'component'=>$val['component'],
                        'sort'=>$val['sort'],
                        'jur'=>1,
                        'rid'=>'',
                        'mid'=>$val['id'],
                        'Icon'=>$val['Icon'],
                        'button'=>array()
                    );
                }
            }
            foreach ($listinfo as $key=>$val){
                if(!empty($val['b_name'])){
                    array_push($jurisdiction[$val['id']]['button'],array('bid'=>'','b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                }
            }
        }

        $return = array();
        $a = 0;
        foreach ($jurisdiction as $key=>$val){
            $return[$a] = $val;
            $a++;
        }

        return $return;
    }


    /*** 管理员权限校验
     * @param $uid int
     * @return array
     */
    public static function checkAdminPower($a_uid){
        if(!empty($a_uid)){
            $user = New User();
            $where_u[] = ["uid",'=',$a_uid];
            $find = $user->info($where_u);
            if(empty($find)){
                return Response::wrapError(0,"无菜单权限");
            }
            else{
                $find = $find->toArray();
                if($find['enable']!=1){
                    return Response::wrapError(0,"无菜单权限");
                }
            }
        }

        return "";

    }


    /*** 菜单列表
     * @param $uid int
     * @return array
     */
    public static function menuListService($data){
        $menu = New Menu();
        $page = $data["page"];
        $num = $data["num"];
        $where[] = ["type","=",1];
        if(isset($data["superior_id"])&&is_numeric($data["superior_id"])){
            if($data["superior_id"] == -1){
                $superior_id = 0;
                $page = 1;
                $num = 50;
            }
            $where[] = ['superior_id','=',$superior_id];
        }

        if(!empty($data["enable"])){
            $where[] = ['enable','=',$data["enable"]];
        }
        if(!empty($data["menu_name"])){
            $where[] = ['menu_name','like','%'.$data["menu_name"].'%'];
        }
        $type = isset($data["type"])?$data["type"]:1;
        $where[] = ['type','=',$type];
        $listinfo = $menu->menulist($page,$num,$where);
        $count = 0;
        if(!empty($listinfo)){
            $listinfo = $listinfo->toArray();
            $count = $menu->menuCount($where);
        }
        else{
            $listinfo = [];
            return [$listinfo,""];
        }
        $jurisdiction = array();

        foreach ($listinfo as $key=>$val){
            if($val['superior_id']!=0){

                if(empty($jurisdiction[$val['id']]) ){
                    // superior_id , menu_name,link,Icon,exhibition, component, sort, b_name, b_iden,b.mid
                    $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                        'superior_id'=>$val['superior_id'],
                        'link'=>$val['link'],
                        'exhibition'=>$val['exhibition'],
                        'component'=>$val['component'],
                        'sort'=>$val['sort'],
                        'jur'=>1,
                        'rid'=>'',
                        'mid'=>$val['id'],
                        'Icon'=>$val['Icon'],
                        'enable'=>$val['enable'],
                        'button'=>array()
                    );
                    if(!empty($val['b_name'])){
                        array_push($jurisdiction[$val['id']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                    }

                }
                else{
                    if(!empty($val['b_name'])){
                        array_push($jurisdiction[$val['id']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                    }
                }
            }
            else{
                $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                    'superior_id'=>0,
                    'link'=>$val['link'],
                    'exhibition'=>$val['exhibition'],
                    'component'=>$val['component'],
                    'sort'=>$val['sort'],
                    'jur'=>'',
                    'rid'=>'',
                    'mid'=>$val['id'],
                    'Icon'=>$val['Icon'],
                    'enable'=>$val['enable'],
                    'button'=>array()
                );
            }
        }

        $return = array();
        $a = 0;
        foreach ($jurisdiction as $key=>$val){
            $return[$a] = $val;
            $a++;
        }
        return [$return,$count];
    }
    /*** 添加菜单
     * @param $data array
     * @return bool
     */
    public static function menuAddService($data){
        $menu = New Menu();
        if(empty($data["superior_id"]) || $data["superior_id"] == -1){
            $data["superior_id"] = 0;
        }
        if(empty($data["exhibition"]) || (int)$data["exhibition"] == -1){
            $data["exhibition"] = 0;
        }
        if($data["enable"] == -1){
            $data["enable"] = 0;
        }
        $data = [
            'superior_id' => $data["superior_id"],
            'menu_name' => $data["menu_name"] ,
            'link' => $data["link"],
            'Icon' => $data["Icon"],
            'exhibition' => $data["exhibition"],
            'component' => $data["component"],
            'enable' => $data["enable"],
            'sort' => $data["sort"],
            'addtime' => time(),
        ];
        $type = isset($data["type"])?$data["type"]:1;
        $data['type'] = $type;
        return $menu->menuAdd($data);
    }

    /*** 修改菜单
     * @param $data array
     * @return bool
     */
    public static function menuEditService($id,$data){
        $menu = New Menu();
        if(empty($data["superior_id"]) || $data["superior_id"] == -1){
            $data["superior_id"] = 0;
        }
        if(empty($data["exhibition"]) || (int)$data["exhibition"] == -1){
            $data["exhibition"] = 0;
        }
        if($data["enable"] == -1){
            $data["enable"] = 0;
        }
        if(!empty($data["menu_name"])){
            $update = [
                'superior_id' => $data["superior_id"],
                'menu_name' => $data["menu_name"] ,
                'link' => $data["link"],
                'component' => $data["component"],
                'Icon' => $data["Icon"],
                'exhibition' => $data["exhibition"],
                'sort' => $data["sort"],
                'enable' => $data["enable"],
            ];
        }
        else{
            $update = [
                'enable' => $data["enable"],
            ];
        }
       return $menu->menuEdit($id,$update);
    }

    /*===============================  角色   ==============================*/

    /*** 角色列表
     * @param $uid int
     * @return array
     */
    public static function roleListService($page,$num,$type){
        $role = new Role();
        $where[] = ["type","=",$type];
        $list = $role->listRole($page,$num,$where);
        foreach ($list as $key => $value) {
            $list[$key]["addtime"] = date( "Y-m-d H:i:s",$list[$key]["addtime"]);
        }
        $count = $role->countRole();
        return [$list,$count];
    }


    /*** 角色添加
     * @param $uid int
     * @return array
     */
    public static function roleAddService($name,$enable){
        $role = New Role();
        $time = time();
        $data = [
            'name' => $name,
            'enable' => $enable,
            'addtime' => $time,
        ];
        return $role->roleAdd($data);
    }

    /*** 角色修改
     * @param $data array
     * @return array
     */
    public static function roleEditService($data){
        $role = New Role();
        if($data["enable"] == -1){
            $data["enable"] = 0;
        }
        $update = ['name' => $data["name"],'enable' => $data["enable"]];
        return $role->roleEdit($data["id"],$update);
    }


    public static function rmListService($a_uid,$rid){
        $role_menu = new Role_menu();

        $where = [];
        $where[] = ['rm.rid','=',$rid];
        $where[] = ['rm.enable','=','1'];

        $return = $role_menu->rmList($where)->toArray();
        return $return;
    }
    /*** 角色权限修改
     * @param $data array
     * @return bool
     */
    public static function rmEditService($rid,$jur){
        $role_menu = new Role_menu();
        $time = time();
        $update_p = ['enable'=>'0'];
        $role_menu->rmEditE($rid,$update_p);
        foreach ($jur as $value) {
            $strarr = explode("-", $value);

            $mid = $strarr[0];
            $bid = $strarr[1];
            $jur = $strarr[2];

            $where = [];
            $where[] = ['rm.rid','=',$rid];
            $where[] = ['rm.mid','=',$mid];
            $where[] = ['rm.bid','=',$bid];
            $return = $role_menu->rmList($where)->toArray();
            if(!empty($return)){
                $update = ['rid' => $rid,'mid' => $mid,'bid' => $bid,'jur' => $jur,'enable'=>'1'];
                $id = $return[0]['id'];
                $role_menu->rmEdit($id,$update);
            }
            else{
                $data = [
                    'rid' => $rid,
                    'mid' => $mid,
                    'bid' => $bid,
                    'jue' => $jur,
                    'addtime' => $time,
                    'enable' => 1
                ];
                $role_menu->rmAdd($data);
            }
        }
    }

    /*** 角色用户列表
     * @param $data array
     * @return bool
     */
    public static function ruListService($a_uid,$uid){
        $roleuser = new RoleUser();
        $where = [];
        $where[] = ['ru.uid','=',$uid];
        $where[] = ['ru.enable','=','1'];
        return $roleuser->ruList($where);
    }

    /*** 角色用户修改、新增
     * @return bool
     */
    public static function ruEditService($rid,$uid){
        $roleuser = new RoleUser();
        $where[] = ['ru.uid','=',$uid];
        $time = time();
        $where[] = ['ru.enable','=','1'];
        $return = $roleuser->ruList($where)->toArray();
        if(!empty($return)){
            $update = ['rid' => $rid,'uid' => $uid];
            $id = $return[0]['id'];
           return $roleuser->ruEdit($id,$update);
            $type = isset($data["type"])?$data["type"]:1;
            $data['type'] = $type;
        }else{
            $data = [
                'rid' => $rid,
                'uid' => $uid,

                'addtime' => $time,
                'enable' => 1
            ];
            $type = isset($data["type"])?$data["type"]:1;
            $data['type'] = $type;
            return $roleuser->ruAdd($data);
        }
    }

}